import {toast} from "../../utils/tool";

const app = getApp();
Page({
    data: {
        activeTab: 0,
        tabList: [
            {label: '全部订单', value: 0,},
            {label: '未支付', value: 10,},
            {label: '已预约', value: 30},
            {label: '使用中', value: 31},
            {label: '已完成', value: 32}
        ],
    },
    tabChange(ev) {
        const {index} = ev.detail
        this.setData({
            activeTab: index
        })
    },
    swiperChange(ev) {
        const {current} = ev.detail
        this.setData({
            activeTab: current
        })
        if (app.globalData.isDev) return
        wx.vibrateShort({
            type: 'heavy',
        })
    },

    // 开大门
    async openBig() {
        console.log(wx.getStorageSync('firststore'))
        let store = wx.getStorageSync('firststore')
        app.ajax({
            url: '/api/frontdoor/openfrontdoor', data: {
                store_id: store[0].id
            }, success: res => {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        })
    },
    // async openDoorBig(e) {
    //     let self = this
    //     //传入1是开大门传入2是开房间门
    //     self.getBlueYaCode(1, e.currentTarget.dataset.id);
    //     // 获取需要写入的数据
    //     setTimeout(() => {
    //         if (this.data.blueYaCode) {
    //             // 初始化蓝牙判断是否开启蓝牙
    //             wx.openBluetoothAdapter({
    //                 success(res) {
    //                     // 执行搜索蓝牙
    //                     setTimeout(() => {
    //                             self.findBlue();
    //                         },
    //                         // 安卓开启蓝牙后，需要300ms延迟
    //                         self.isModel() == 'android' ? this.data.delayTime : 0
    //                     )
    //                 },
    //                 fail(err) {
    //                     wx.showToast({
    //                         title: '请开启蓝牙,部分安卓手机需要开启定位服务',
    //                         icon: 'none',
    //                         duration: 800
    //                     })
    //                     return
    //                 }
    //             })
    //         }
    //     }, 1000)
    // },
    // getBlueYaCode(type, orderId) {
    //     let self = this
    //     app.ajax({
    //         url: '/api/lock/open',
    //         data: {
    //             type: type,
    //             orderId: orderId
    //         },
    //         success(res) {
    //             self.setData({
    //                 lockId: res.data.lockId,
    //                 blueYaCode: res.data.key
    //             })
    //         }
    //     })
    // },
    close(e) {
        wx.showModal({
            title: '您确定提前结束吗?',
            success: ({cancel}) => {
                if (cancel) return
                let self = this;
                const {detail: id} = e
                app.ajax({
                    url: '/api/user/earlyClose',
                    data: {
                        order_id: id.toString().trim()
                    },
                    success: res => {
                        if (res.code === 1) {
                            toast({title: '操作成功'})
                            this.resetData()
                        }
                    }
                })
            }
        })
    },
    async openDoorSmall(e) {
        const {status, id} = e.detail
        if (Number(status) === 31) return this.openLockHandler(id);
        wx.showModal({
            title: '提前开启',
            content: '是否提前开启包间？结束时间也将提前结束',
            success: (res) => {
                if (res.cancel) return
                this.openLockHandler(id)
                // app.ajax({
                //     url: "/api/user/earlyOpening",
                //     data: {order_id: id},
                //     success: (resp) => {
                //         const {code, msg} = resp
                //         if (code !== 1) return toast({title: msg})
                //
                //     }
                // })
            }
        })
    },
    resetData() {
        const {activeTab} = this.data
        const target = this.selectComponent('#list_' + activeTab)
        if (!target) return
        target.initData(false)
    },
    openLockHandler(id) {
        app.ajax({
            url: '/api/lock/open',
            data: {
                orderId: id
            },
            success: res => {
                const {msg} = res
                toast({title: msg})
                this.resetData()
                // self.setData({
                //     lockId: res.data.lockId,
                //     blueYaCode: res.data.key
                // })
            }
        })
    },
    cancelOrder(e) {
        wx.showModal({
            title: '您确认取消订单吗?',
            success: res => {
                if (res.cancel) return
                const {detail: id} = e
                app.ajax({
                    url: "/api/user/canalOrder",
                    data: {order_id: id},
                    success: resp => {
                        const {msg} = resp
                        toast({title: msg})
                        this.resetData()
                    }
                })
            }
        })
    },

    unsettled: function (e) {
        const {detail: id} = e
        wx.navigateTo({
            url: '/pages/unsettled/unsettled?order_id=' + id
        })
    },
    opening_code: function (e) {
        const {detail: id} = e
        wx.navigateTo({
            url: '/pages/opening_code/opening_code?order_id=' + id
        })
    },

    used: function () {
        wx.navigateTo({
            url: '/pages/used/used',
        })
    },
    expired: function () {
        wx.navigateTo({
            url: '/pages/expired/expired',
        })
    },
    // setStatus: function (e) {
    //     const status = e.currentTarget.dataset.status;
    //     if (this.data.status != status) {
    //         this.setData({
    //             status: status
    //         });
    //         this.getList(1);
    //     }
    // },

    getList: function (reset) {
        if (reset) {
            this.setData({
                page: 1,
                list: [],
                is_ajax: false,
                is_over: false
            });
        }
        // if (this.data.is_ajax || this.data.is_over) return false;
        // this.setData({
        //     is_ajax: true
        // });
        var page = this.data.page,
            status = this.data.status,
            list = this.data.list,
            is_ajax = true,
            is_over = false;
        app.ajax({
            url: '/api/user/orderList',
            data: {
                status: status,
                page: page
            },
            success: res => {
                if (res.code == 1) {
                    page++;
                    list = list.concat(res.data);
                    var now = parseInt(Date.now() / 1000);
                    for (var i in list) {
                        if (list[i].status == 10) {
                            list[i].gopay_time = parseInt((list[i].create_time + 180 - now) /
                                60);
                            if (list[i].gopay_time < 0) list[i].gopay_time = 0;
                        }
                    }
                } else {
                    is_over = true;
                }
                is_ajax = false;
                this.setData({
                    page: page,
                    list: list,
                    is_ajax: is_ajax,
                    is_over: is_over
                });
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList();
    },
    onShareAppMessage: function (res) {
        const {dataset: {id}} = res.target
        return {
            title: '亲 赠送给你一个茶室包间请 前去消费',
            path: `/pages/opening_code/opening_code?type=1&order_id=${id}`, //这里是被分享的人点击进来之后的页面
        }
    },

})
